package org.s170.utils;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Dbutil {
    //驱动
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    //服务器和数据库
    private static final String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=foodShareDB";
    //用户名
    private static final String DB_USER = "sa";
    //密码
    private static final String DB_PWD = "123456";

    //加载驱动
    static {
        try {
            Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //通过驱动创建一个连接对象并返回
    public static Connection getConnection() {
        try {
            Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取连接对象错误", e);
        }
    }

    //关闭连接对象的方法
    public static void closeAll(Connection conn, PreparedStatement psmt, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (psmt != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 用于执行非查询的sql语句的方法
     *
     * @param sql        非查询的SQL语句
     * @param parameters SQL语句中的参数
     * @return 返回受影响的行数
//     * @throws SQLExection异常
     */
    public static int executeUpdate(String sql, Object... parameters) {
        Connection conn = null;
        PreparedStatement psmt = null;
        try {
            conn = Dbutil.getConnection();
            psmt = conn.prepareStatement(sql);
            //设置sql中的参数
            for (int i = 0; i < parameters.length; i++) {
                psmt.setObject(i + 1, parameters[i]);
            }
            //执行sql语句,返回受影响的函数
            int rows = psmt.executeUpdate();
            return rows;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbutil.closeAll(conn, psmt, null);
        }
        return -1;
    }

    //格式化时间类型
    public static  String dateFormat(){
        Date date=new Date();
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String strDate = formatter.format(date);
        return strDate;
    }
    //字符串时间转换为Date
    public static  Date datePares(String time)  {
       try {
           SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd");
           Date date=ft.parse(time);  //不写try要抛出一个转换异常
           return date;
       } catch (ParseException e){
           e.printStackTrace();
       }
        return new Date();
    }


    public static void main(String[] args) {
        System.out.println(Dbutil.getConnection() + "哈哈");
    }
}
